package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //保存菜品的基本信息到dish表
        dishMapper.insert(dish);

        //获得新增菜品的id
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null&&flavors.size()>0) {
            //向口味表中插入n条数据
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertList(flavors);
        }
    }

    /**
     * @param dishPageQueryDTO
     * @return
     */
    //分页查询
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishPage =dishMapper.page(dishPageQueryDTO);
        PageResult pageResult =new PageResult(dishPage.getTotal(), dishPage.getResult());
        return pageResult;
    }

    /**
     * @param ids
     */

    //批量删除菜品
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否能够删除----是否存在起售中
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == 1) {
                //起售中
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否能够删除----是否被关联
        List<Long> setmealIdByDishId =setmealDishMapper.selectSetmealIdByDishId(ids);
        if (setmealIdByDishId != null && setmealIdByDishId.size() > 0) {
            //被关联
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品表中的菜品数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品口味表中的数据
            dishFlavorMapper.deleteByDishId(id);
        }

    }

    /**
     * @param id
     * @return
     */
    @Override
    @Transactional
    public DishVO getById(Long id) {
        Dish dish =dishMapper.selectById(id);
        List<DishFlavor> flavors =dishFlavorMapper.selectByDishId(id);
        DishVO dishVO =new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * @param dishDTO
     */
    //修改菜品基本信息和口味信息
    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO){
        Dish dish =new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);


        //删除原有的口味数据
        Long dishId = dishDTO.getId();
        dishFlavorMapper.deleteByDishId(dishId);

        //插入新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDTO.getId());
            dishFlavorMapper.insert(flavor);
        }
    }
    }

    //根据分类id查询菜品信息和口味信息
    @Override
    public List<DishVO> selectByCategoryId(Dish dish) {
        //根据分类id查询菜品信息
        List<Dish> dishes=dishMapper.selectByCategoryId(dish);

        List<DishVO> list = new ArrayList<>();
        for (Dish dish1 : dishes) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish1, dishVO);
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dish1.getId());
            dishVO.setFlavors(flavors);
            list.add(dishVO);
        }
        return list;
    }

    //修改菜品状态
    public void updateStatus(Integer status, Long id) {
        dishMapper.updateStatus(status, id);
    }

    /**
     * @param categoryId
     * @return
     */
    @Override
    //根据分类id查询菜品信息
    public List<Dish> list(Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        return dishMapper.selectByCategoryId(dish);
    }

}
